/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import model.dao.DaoBase;
import model.dao.FaltaJpaController;
import model.pojo.Aluno;
import model.pojo.Atividade;
import model.pojo.Falta;
import model.pojo.Professor;
import model.pojo.Turma;

/**
 *
 * @author leonidia
 */
public class CadastrarFaltas extends javax.swing.JFrame {

    FaltaJpaController faltaJpaController = null;
    Professor professor;
    List<Atividade> listaA;
    List<Aluno> alunos;
    List<Turma> listaT;

    /**
     * Creates new form CadastrarFalta
     * @param daoBase Cadastra os faltas do aluno e a envia para o faltaJpaController inserir elas no BD.
     */
    public CadastrarFaltas(DaoBase daoBase) {
        initComponents();
        faltaJpaController = new FaltaJpaController(null, daoBase.returnFactory());
        this.professor = professor;
        atualizaTurma();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        Cadastrar = new javax.swing.JButton();
        Turma = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TabelaFaltas = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        jLabel4.setText("jLabel4");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        Cadastrar.setText("Cadastrar");
        Cadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CadastrarActionPerformed(evt);
            }
        });

        Turma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TurmaActionPerformed(evt);
            }
        });

        jLabel1.setText("Cadastrar Faltas");

        TabelaFaltas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome do Aluno", "Matricula", "Faltas"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(TabelaFaltas);

        jLabel2.setText("Turmas");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Turma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(132, 132, 132)
                                .addComponent(jLabel1))
                            .addComponent(jLabel3)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(203, 203, 203)
                        .addComponent(Cadastrar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(jLabel2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Turma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addGap(43, 43, 43)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(Cadastrar)
                .addGap(0, 92, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CadastrarActionPerformed
        // TODO add your handling code here:
        for (int linha = 0; linha < TabelaFaltas.getRowCount(); linha++) {
            Falta falta = new Falta(Integer.parseInt((String) TabelaFaltas.getValueAt(0, 2)), listaT.get(Turma.getSelectedIndex()), alunos.get(linha));
            faltaJpaController.create(falta);
        }
        dispose();
    }//GEN-LAST:event_CadastrarActionPerformed

    private void TurmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TurmaActionPerformed
        // TODO add your handling code here:
        atualizaTabela(listaT.get(Turma.getSelectedIndex()));
    }//GEN-LAST:event_TurmaActionPerformed

    /**
     * @param args the command line arguments
     */
    public void run() {
        setVisible(true);
    }

    public void stop() {
        setVisible(false);
    }

    private void atualizaTabela(Turma turma) {
        DefaultTableModel model = (DefaultTableModel) TabelaFaltas.getModel();
        alunos = (List<Aluno>) turma.getAluno();
        for (Aluno item : alunos) {
            model.addRow(new Object[]{item.getNome(), item.getMatricula(), null});
        }
    }

    public void atualizaTurma() {
        List<String> listaTurma = new ArrayList<String>();
        listaT = (List<Turma>) professor.getTurma();
        for (Turma item : listaT) {
            listaTurma.add("Ano: " + item.getAno() + " Local: " + item.getLocal() + " Disciplina: " + item.getDisciplina().getNome());
        }
        DefaultComboBoxModel defaultProfessor = new DefaultComboBoxModel(listaTurma.toArray());
        Turma.setModel(defaultProfessor);
    }

    public void atualizaNota(Turma turma) {
        List<String> listaAtividade = new ArrayList<String>();
        listaA = (List<Atividade>) turma.getAtividade();
        for (Atividade item : listaA) {
            listaAtividade.add("Nome: " + item.getNome() + " Tipo: " + item.getTipo() + " Valor: " + item.getValor());
        }
        DefaultComboBoxModel defaultAtividade = new DefaultComboBoxModel(listaAtividade.toArray());
        Turma.setModel(defaultAtividade);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cadastrar;
    private javax.swing.JTable TabelaFaltas;
    private javax.swing.JComboBox Turma;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}